﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[resuma_PreluareDate]
@codUnitateDest smallint,@CodUnitateSursa smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	--SET NOCOUNT ON;
	BEGIN TRAN
    -- Insert statements for procedure here


	PRINT('Situatie Militara')
	DELETE FROM [SituatieMilitara] FROM [Persoane] p WHERE p.IDPersoana = [SituatieMilitara].[IDPersoana]
		AND p.[CodUnitate]=@CodUnitateDest
	IF @@ERROR != 0 GOTO EROARE

	PRINT('Polita Asigurare')
	DELETE FROM [PolitaAsigurare] FROM Persoane p WHERE p.IdPersoana = [PolitaAsigurare].IDPersoana
		AND p.[CodUnitate]=@CodUnitateDest
	IF @@ERROR != 0 GOTO EROARE

	PRINT('Contracte Munca')
	DELETE FROM [ContracteMunca] WHERE [CodUnitate]=@CodUnitateDest
	IF @@ERROR != 0 GOTO EROARE
	
	PRINT('Persoane')
	DELETE FROM [Persoane] WHERE [CodUnitate]=@CodUnitateDest
	IF @@ERROR != 0 GOTO EROARE

	PRINT('Setup')
	DELETE FROM [ResumaSetup] WHERE [CodUnitate]=@CodUnitateDest
	IF @@ERROR != 0 GOTO EROARE
	
	PRINT('TipStareMilitara')
	DELETE FROM [TipStareMilitara]
	IF @@ERROR != 0 GOTO EROARE

	PRINT('TipGradMilitar')
	DELETE FROM [TipGradMilitar]
	IF @@ERROR != 0 GOTO EROARE
	
	PRINT('import TipGradMilitar')
	SET IDENTITY_INSERT [TipGradMilitar] ON

	INSERT INTO [TipGradMilitar] ([IdTipGradMilitar],[Denumire])
	SELECT [IdTipGradMilitar],[Denumire]
	FROM [ESRP_GOL].[dbo].[TipGradMilitar]
	IF @@ERROR != 0 GOTO EROARE1

	SET IDENTITY_INSERT [TipGradMilitar] OFF
	PRINT('import TipStareMilitara')
	SET IDENTITY_INSERT [TipStareMilitara] ON

	INSERT INTO [TipStareMilitara] ([IdStareMilitara],[Denumire])
	SELECT [IdStareMilitara],[Denumire]
	FROM [ESRP_GOL].[dbo].[TipStareMilitara]
	IF @@ERROR != 0 GOTO EROARE1

	SET IDENTITY_INSERT [TipStareMilitara] OFF

	PRINT('import [ResUmaSetup]')

	INSERT INTO [ResUmaSetup] ([PrimaryKey]
      ,[CodUnitate]
      ,[AnulCurent]
      ,[LunaCurenta]
      ,[PropagaModificari]
      ,[Contracte])
	SELECT [PrimaryKey]
      ,[CodUnitate]
      ,[AnulCurent]
      ,[LunaCurenta]
      ,[PropagaModificari]
      ,[Contracte]
	FROM [ESRP_GOL].[dbo].[ResUmaSetup] where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1

	PRINT('import [Persoane]')
	SET IDENTITY_INSERT [Persoane] ON

	INSERT INTO [Persoane] ([IdPersoana]
      ,[NumePrenume]
      ,[Nume_Anterior]
      ,[Cnp]
      ,[Sex]
      ,[DataNasterii]
      ,[IDLocNastere]
      ,[TipAI]
      ,[SeriaAI]
      ,[NumarAI]
      ,[DataElibAI]
      ,[EmitentAI]
      ,[NumeTata]
      ,[NumeMama]
      ,[StradaAdr]
      ,[IDLocalitateAdr]
      ,[NrAdr]
      ,[BlocAdr]
      ,[EtajAdr]
      ,[ScaraAdr]
      ,[ApartamentAdr]
      ,[GradInvaliditate]
      ,[TelefonFix]
      ,[TelefonMobil]
      ,[Observatii]
      ,[NumarCM]
      ,[SerieCM]
      ,[CodUnitate]
      ,[NrFileCM]
      ,[NrCutieCM])
	SELECT [IdPersoana]
      ,[NumePrenume]
      ,[Nume_Anterior]
      ,[Cnp]
      ,[Sex]
      ,[DataNasterii]
      ,[IDLocNastere]
      ,[TipAI]
      ,[SeriaAI]
      ,[NumarAI]
      ,[DataElibAI]
      ,[EmitentAI]
      ,[NumeTata]
      ,[NumeMama]
      ,[StradaAdr]
      ,[IDLocalitateAdr]
      ,[NrAdr]
      ,[BlocAdr]
      ,[EtajAdr]
      ,[ScaraAdr]
      ,[ApartamentAdr]
      ,[GradInvaliditate]
      ,[TelefonFix]
      ,[TelefonMobil]
      ,[Observatii]
      ,[NumarCM]
      ,[SerieCM]
      ,[CodUnitate]
      ,[NrFileCM]
      ,[NrCutieCM]
	FROM [ESRP_GOL].[dbo].[Persoane]
	where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1
	SET IDENTITY_INSERT [Persoane] OFF


	PRINT('import [ContracteMunca]')
	SET IDENTITY_INSERT [ContracteMunca] ON

	INSERT INTO [ContracteMunca] ([IdContract]
      ,[IdPersoana]
      ,[Numar]
      ,[Data]
      ,[PerioadaScadenta]
      ,[TipContract]
      ,[DeLaData]
      ,[PanaLaData]
      ,[PerioadaDeterminata]
      ,[IDOcupatie]
      ,[TipGrad]
      ,[SalarIncadrare]
      ,[Observatii]
      ,[CodUnitate]
      ,[OreNorma]
      ,[FractiuneNorma])
	SELECT [IdContract]
      ,[IdPersoana]
      ,[Numar]
      ,[Data]
      ,[PerioadaScadenta]
      ,[TipContract]
      ,[DeLaData]
      ,[PanaLaData]
      ,[PerioadaDeterminata]
      ,[IDOcupatie]
      ,[TipGrad]
      ,[SalarIncadrare]
      ,[Observatii]
      ,[CodUnitate]
      ,[OreNorma]
      ,[FractiuneNorma]
	FROM [ESRP_GOL].[dbo].[ContracteMunca]
	where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1
	SET IDENTITY_INSERT [ContracteMunca] OFF


	PRINT('import [Angajati]')
	SET IDENTITY_INSERT [Angajati] ON

	INSERT INTO [Angajati] ([IDAngajat]
      ,[IdPersoana]
      ,[IdTipSpecialitate]
      ,[Marca]
      ,[Salar]
      ,[IDContract]
      ,[IDOcupatie]
      ,[IDDepartament]
      ,[TelefonInterior]
      ,[CodUnitate]
      ,[VechimeMuncaAni]
      ,[VechimeMuncaLuni]
      ,[VechimeSpecialitateAni]
      ,[VechimeSpecialitateLuni]
      ,[IDOcupatieSecundara]
      ,[IdTipGrad]
      ,[Luna]
      ,[Anul]
      ,[OreNorma]
      ,[FractiuneNorma]
      ,[IdCasaAsigSanatate]
      ,[IdTipStudii]
      ,[IdTipSpecialitateSecundara])
	SELECT [IDAngajat]
      ,[IdPersoana]
      ,[IdTipSpecialitate]
      ,[Marca]
      ,[Salar]
      ,[IDContract]
      ,[IDOcupatie]
      ,[IDDepartament]
      ,[TelefonInterior]
      ,[CodUnitate]
      ,[VechimeMuncaAni]
      ,[VechimeMuncaLuni]
      ,[VechimeSpecialitateAni]
      ,[VechimeSpecialitateLuni]
      ,[IDOcupatieSecundara]
      ,[IdTipGrad]
      ,[Luna]
      ,[Anul]
      ,[OreNorma]
      ,[FractiuneNorma]
      ,[IdCasaAsigSanatate]
      ,[IdTipStudii]
      ,[IdTipSpecialitateSecundara]
	FROM [ESRP_GOL].[dbo].[Angajati]
	where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1
	SET IDENTITY_INSERT [Angajati] OFF


	PRINT('import []')
	SET IDENTITY_INSERT [PolitaAsigurare] ON

	INSERT INTO [PolitaAsigurare] ([IDPolitaAsigurare]
      ,[IDAngajat]
      ,[Serie]
      ,[Numar]
      ,[DataValabilitate]
      ,[SocAsigurare])
	SELECT [IDPolitaAsigurare]
      ,pa.[IDAngajat]
      ,[Serie]
      ,[Numar]
      ,[DataValabilitate]
      ,[SocAsigurare]
	FROM [ESRP_GOL].[dbo].[PolitaAsigurare] pa JOIN [ESRP_GOL].[dbo].[Angajati] pp ON pa.[IDAngajat]=pp.[IDangajat]
	where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1
	SET IDENTITY_INSERT [PolitaAsigurare] OFF

	PRINT('import [SituatieMilitara]')

	INSERT INTO [SituatieMilitara] ([IdPersoana]
      ,[IdGradMilitar]
      ,[Specialitatea]
      ,[Pozitia]
      ,[IdStareMilitara]
      ,[DataScoatereEvidenta]
      ,[DataLuatEvidenta]
      ,[SeriaLM]
      ,[NumarLM]
      ,[LocLM]
      ,[CorpMilitar])
	SELECT sm.[IdPersoana]
      ,[IdGradMilitar]
      ,[Specialitatea]
      ,[Pozitia]
      ,[IdStareMilitara]
      ,[DataScoatereEvidenta]
      ,[DataLuatEvidenta]
      ,[SeriaLM]
      ,[NumarLM]
      ,[LocLM]
      ,[CorpMilitar]
	FROM [ESRP_GOL].[dbo].[SituatieMilitara] sm JOIN [ESRP_GOL].[dbo].[Persoane] pp ON pp.[IDPersoana]=sm.[IDPersoana]
	where [CodUnitate] = @CodUnitateSursa
	IF @@ERROR != 0 GOTO EROARE1


	--PRINT('import []')
	--SET IDENTITY_INSERT  ON

	--INSERT INTO  ()
	--where [CodUnitate] = @CodUnitateSursa
	--IF @@ERROR != 0 GOTO EROARE1
	--SET IDENTITY_INSERT  OFF


	COMMIT

GOTO Gata
EROARE:
	ROLLBACK
	RAISERROR ('Eroare la inchiderea anului!',16,-1)
	GOTO Gata
EROARE1:
	RAISERROR ('!!',16,-1)	
	GOTO Gata
Gata:
END